<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Kategori extends MY_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
    public function __construct()
    {
        parent::__construct();
            // Your own constructor code
        
      
    } 
    
 public function tq3($category_id , $subcategory_id)
    {
    /*    $this->db->from(array('products','brands'));
     $this->db->select(array('brands.name','brands.brands_id'));
     
     
     
     
    
     $this->db->join('brands','brands.brand_id = products.brand_id');
     $this->db->where('products.category_id' , $category_id);
     $this->db->where('products.subcategory_id', $subcategory_id);
     $this->db->where('products.brand_id','brands.brand_id');
     $data =$this->db->get()->result_array();
     echo $this->db->last_query();*/
  //   $data = $this->db->query("SELECT brands.brand_id, brands.name FROM `products` , brands WHERE products.category_id = $category_id and products.subcategory_id = $subcategory_id and products.brand_id = brands.brand_id")->result_array();
    //$data = $this->db->get('brands')->result_array();  


        
/*
        $this->db->select('DISTINCT brands.brand_id, brands.name');

        $this->db->from('products');

        $this->db->from('brands');

       

        //$this->db->join('seller','seller.product_id = products.product_id');

        //$this->db->join('talepler','talepler.talep_user_id = seller.user_id');

        $this->db->where(array('products.category_id' => $category_id, 'products.subcategory_id' => $subcategory_id, 'products.brand_id' => 'brands.brand_id'));

       

        $result = $this->db->get()->result();*/
      

        

  $sql = "select DISTINCT brands.brand_id, brands.name from products, brands where products.category_id = $category_id and products.subcategory_id = $subcategory_id and products.brand_id = brands.brand_id ";

        $data = $this->db->query($sql)->result_array();

        echo json_encode( $data );
    }

    public function tq2($category_id)
    {
        $data = $this->db->select(array('name','subcategories_id'))->where('category_id' ,$category_id)->get('subcategories')->result_array();
       /* echo "<pre>";
        var_dump($data);
    die();*/
        echo json_encode( $data );
    }

    public function tq()
    {
        $data = $this->db->select(array('name','category_id'))->get('categories')->result_array();
       /* echo "<pre>";
        var_dump($data);
    die();*/
        echo json_encode( $data );
    }


    	public function kategoriler()

	{

        $this->load->view('kategori');
    //  
       /* $this->db->update('products', array(

            'image1' => "http://tuna.icmproje.com/public/uploads/urun_1.jpg",

            'image2' => "http://tuna.icmproje.com/public/uploads/urun_1.jpg",

            'image3' =>  "http://tuna.icmproje.com/public/uploads/urun_2.jpg",

            'image4'   => "http://tuna.icmproje.com/public/uploads/urun_3.jpg",

            'image5'    => "http://tuna.icmproje.com/public/uploads/urun_4.jpg"

        ));*/

	}
        	public function kategoriler2()

	{

        $this->load->view('kategori2');
    //  
       /* $this->db->update('products', array(

            'image1' => "http://tuna.icmproje.com/public/uploads/urun_1.jpg",

            'image2' => "http://tuna.icmproje.com/public/uploads/urun_1.jpg",

            'image3' =>  "http://tuna.icmproje.com/public/uploads/urun_2.jpg",

            'image4'   => "http://tuna.icmproje.com/public/uploads/urun_3.jpg",

            'image5'    => "http://tuna.icmproje.com/public/uploads/urun_4.jpg"

        ));*/

	}
    public function index()
    {  $data['subview'] = 'new_categories_list';
        $this->load->view('layouts/default1',$data);
        
    }
    public function satis_yap()
    {  $data['subview'] = 'new_categories_list_satis_yap';
        $this->load->view('layouts/default1',$data);
        
    }    
public function kategori_sec()
{
  
    if($this->input->post('category_id') == "" || $this->input->post('subcategory_id') == "" || $this->input->post('brand_id') == "")
    {
  
       $data['subview'] = 'new_categories_list';
       $data['error'] = "<small>LUTFEN BUTUN ALANLARI SECTIGINIZDEN EMIN OLUN.</small>";
        $this->load->view('layouts/default1',$data);
    }
    else
    {
       redirect(base_url().'kategori/satis_yap_list/'.$this->input->post('category_id').'/'.$this->input->post('subcategory_id').'/'.$this->input->post('brand_id').'/1','location', 301);
    
    }

                
            
    //$this->output->set_header('Location: ' .base_url().'kategoriler/'.$this->input->post('category_id').'/'.$this->input->post('subcategory_id').'/'.$this->input->post('brand_id')); 
    
}
public function satis_yap_list($category_id,$subcategory_id,$brand_id,$satis_type)
{
        // satis type = 0 ->normal satis
        // satis type = 1 ->hizli satis
                $category_id = $category_id; //$this->mycategories->get_category_name($this->config_model->get_categories()->result(),$category_name);
                $subcategory_id = $subcategory_id; //$this->mycategories->get_subcategory_name($this->config_model->get_subcategories($category_id)->result(),$subcategory_name);
                $brand_id = $brand_id; //$this->mycategories->get_brand_name($this->config_model->get_brands()->result(),$brand_name);
              
                $data['category_id'] = $category_id;
                $data['category_name'] = $this->config_model->get_category_name($category_id)->row(0)->name;
                
                $data['subcategory_id'] = $subcategory_id;
                $data['subcategory_name'] = $this->config_model->get_subcategory_name($subcategory_id)->row(0)->name;
                
                $data['brand_id'] = $brand_id;
                $data['brand_name'] = $this->config_model->get_brand_name($brand_id)->row(0)->name;
                
                $pag_url = base_url().'kategori/satis_yap_list/'.$category_id."/".$subcategory_id."/".$brand_id.'/'.$satis_type;
                $per_page = $this->_pagination($category_id,$subcategory_id,$brand_id,$pag_url);
               // $data['list_products'] = $this->_capture_products($category_id,$subcategory_id,$brand_id, $per_page,$this->uri->segment(6));
                
                
                $this->db->limit($per_page, $this->uri->segment(7));
                $data['products'] =     $this->db->where(array('brand_id' => $brand_id , 'category_id' => $category_id , 'subcategory_id' => $subcategory_id ))->get('products')->result();
                
            /*    echo "<pre>";
                var_dump($data['products']);
                die();*/
                if($satis_type    == "1")
                {
                    $data['subview'] = 'view_list_products2_hizli_sat';
                   $this->load->view('layouts/default1',$data);  
                }
                else
                {
                   $data['subview'] = 'view_list_products2_satis_yap';
                   $this->load->view('layouts/default1',$data);  
                }
}
  public function _pagination($category_id,$subcategory_id,$brand_id,$page_url)
    {
  		
		$config['base_url'] = $page_url;
	
        $config['total_rows'] = $this->db->where(array('brand_id' => $brand_id , 'category_id' => $category_id , 'subcategory_id' => $subcategory_id ))->get('products')->num_rows();
		$config['per_page'] = 12;
	//	$config['num_links'] = 2;
        $config['uri_segment'] = '7';
        //$config['page_query_string'] = TRUE;
   //     $config['use_page_numbers'] = TRUE;
        $config['next_link'] = 'Sonraki';
        $config['next_tag_open'] = '<li>';
        $config['next_tag_close'] = '</li>';
        $config['prev_link'] = 'Önceki';
        $config['prev_tag_open'] = '<li>';
        $config['prev_tag_close'] = '</li>';        
        
        $config['cur_tag_open'] = '<li class="active"><a href="#">';
        $config['cur_tag_close'] = '</a></li>';
        $config['first_link'] = 'İlk';
        $config['first_tag_open'] = '<li>';
        $config['first_tag_close'] = '</li>';
        $config['last_link'] = 'Son';
        $config['last_tag_open'] = '<li>';
        $config['last_tag_close'] = '</li>';  
		$config['full_tag_open'] = ' <div class="pagination pull-right"><ul>';
		$config['full_tag_close'] = '</ul></div>';
        $config['num_tag_open'] = '<li>';
        $config['num_tag_close'] = '</li>';
	
		$this->pagination->initialize($config);
        return $config['per_page'];
		
        
		
    }    
public function kategori_sec2()
{
  // normal satis
    if($this->input->post('category_id') == "" || $this->input->post('subcategory_id') == "" || $this->input->post('brand_id') == "")
    {
  
       $data['subview'] = 'new_categories_list';
       $data['error'] = "<small>LUTFEN BUTUN ALANLARI SECTIGINIZDEN EMIN OLUN.</small>";
        $this->load->view('layouts/default1',$data);
    }
    else
    {
        redirect(base_url().'kategori/satis_yap_list/'.$this->input->post('category_id').'/'.$this->input->post('subcategory_id').'/'.$this->input->post('brand_id').'/0','location', 301);
    }

                
            
    //$this->output->set_header('Location: ' .base_url().'kategoriler/'.$this->input->post('category_id').'/'.$this->input->post('subcategory_id').'/'.$this->input->post('brand_id')); 
    
}
    function arama()
    {
         $this->db->insert('ci_query', array('value' => $this->input->post('k')));
	     $query_id = $this->db->insert_id();
         redirect("kategori/display/$query_id");
    }
    function display($query_id = 0,$sort_by = 'model', $sort_order = 'asc', $offset = 0)
    {
        if($this->session->userdata('goster'))
        {
            $limit = $this->session->userdata('goster');
        }
        else
        {
            $limit = 10;
        }
        
        $this->load->model('Search_model');
        $query_array = $this->db->where('id',$query_id)->get('ci_query')->row(0);
      

        

       
          
        
        $results = $this->Search_model->kategori_hizli_search($query_array,$limit, $offset, $sort_by, $sort_order,0);
     /*  echo "<pre>";
        var_dump($results->result());
        echo $this->db->last_query();
        die(); */
        
        
        $data['query_id'] = $query_id;
        $this->load->library('pagination');
		$config = array();
		$config['base_url'] = site_url("kategori/display/$query_id/$sort_by/$sort_order");
		$config['total_rows'] = $this->Search_model->kategori_hizli_search($query_array,$limit, $offset, $sort_by, $sort_order,1)->num_rows();
		$config['per_page'] = $limit;
		$config['uri_segment'] = 6;
        
        $config['next_link'] = 'Sonraki';
        $config['next_tag_open'] = '<li>';
        $config['next_tag_close'] = '</li>';
        $config['prev_link'] = 'Önceki';
        $config['prev_tag_open'] = '<li>';
        $config['prev_tag_close'] = '</li>';        
        
        $config['cur_tag_open'] = '<li class="active"><a href="#">';
        $config['cur_tag_close'] = '</a></li>';
        $config['first_link'] = 'İlk';
        $config['first_tag_open'] = '<li>';
        $config['first_tag_close'] = '</li>';
        $config['last_link'] = 'Son';
        $config['last_tag_open'] = '<li>';
        $config['last_tag_close'] = '</li>';  
		$config['full_tag_open'] = ' <div class="pagination pull-right"><ul>';
		$config['full_tag_close'] = '</ul></div>';
        $config['num_tag_open'] = '<li>';
        $config['num_tag_close'] = '</li>';
        
      
		$this->pagination->initialize($config);
		
	
		$data['sort_by'] = $sort_by;
		$data['sort_order'] = $sort_order;
		$data['pagination'] = $this->pagination->create_links();
		$data['products'] = $results->result();
        $data['total'] = $this->Search_model->kategori_hizli_search($query_array, $limit, $offset, $sort_by, $sort_order,1)->num_rows();
        $data['all_products'] = $this->Search_model->kategori_hizli_search($query_array,$limit, $offset, $sort_by, $sort_order,1)->result();
        $data['subview'] = 'hizli_al_sat/view_hizlisat_search';
        $this->load->view('layouts/default1',$data); 
                    
                    
    }
     
    public function arama2($value = NULL)
    {
        $this->db->insert('ci_query', array('value' => $this->input->post('k')));
	     $query_id = $this->db->insert_id();
         redirect("kategori/display_normal/$query_id");
                    
    }
    function display_normal($query_id = 0,$sort_by = 'model', $sort_order = 'asc', $offset = 0)
    {
        if($this->session->userdata('goster'))
        {
            $limit = $this->session->userdata('goster');
        }
        else
        {
            $limit = 10;
        }
        
        $this->load->model('Search_model');
        $query_array = $this->db->where('id',$query_id)->get('ci_query')->row(0);
      

        

       
          
        
        $results = $this->Search_model->kategori_normal_search($query_array,$limit, $offset, $sort_by, $sort_order,0);
     /*  echo "<pre>";
        var_dump($results->result());
        echo $this->db->last_query();
        die(); */
        
        
        $data['query_id'] = $query_id;
        $this->load->library('pagination');
		$config = array();
		$config['base_url'] = site_url("kategori/display_normal/$query_id/$sort_by/$sort_order");
		$config['total_rows'] = $this->Search_model->kategori_normal_search($query_array,$limit, $offset, $sort_by, $sort_order,1)->num_rows();
		$config['per_page'] = $limit;
		$config['uri_segment'] = 6;
        
        $config['next_link'] = 'Sonraki';
        $config['next_tag_open'] = '<li>';
        $config['next_tag_close'] = '</li>';
        $config['prev_link'] = 'Önceki';
        $config['prev_tag_open'] = '<li>';
        $config['prev_tag_close'] = '</li>';        
        
        $config['cur_tag_open'] = '<li class="active"><a href="#">';
        $config['cur_tag_close'] = '</a></li>';
        $config['first_link'] = 'İlk';
        $config['first_tag_open'] = '<li>';
        $config['first_tag_close'] = '</li>';
        $config['last_link'] = 'Son';
        $config['last_tag_open'] = '<li>';
        $config['last_tag_close'] = '</li>';  
		$config['full_tag_open'] = ' <div class="pagination pull-right"><ul>';
		$config['full_tag_close'] = '</ul></div>';
        $config['num_tag_open'] = '<li>';
        $config['num_tag_close'] = '</li>';
        
      
		$this->pagination->initialize($config);
		
	
		$data['sort_by'] = $sort_by;
		$data['sort_order'] = $sort_order;
		$data['pagination'] = $this->pagination->create_links();
		$data['products'] = $results->result();
        $data['total'] = $this->Search_model->kategori_normal_search($query_array, $limit, $offset, $sort_by, $sort_order,1)->num_rows();
        $data['all_products'] = $this->Search_model->kategori_normal_search($query_array,$limit, $offset, $sort_by, $sort_order,1)->result();
        $data['subview'] = 'hizli_al_sat/view_normal_search';
        $this->load->view('layouts/default1',$data); 
                    
                    
    }
         
     
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */